#include "shot.h"
#include "bubu.h"

Shot::Shot(int attack) : atk(attack)
{
    atk = attack;
    speed = 360.0 * 66 / 1000;
}

QRectF Shot::boundingRect() const
{
    return QRectF(-12, -28, 24, 24);
}

void Shot::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option)
    Q_UNUSED(widget)
    painter->drawPixmap(QRect(-12, -28, 24, 24), QPixmap(":/picture/shot.png"));
}

bool Shot::collidesWithItem(const QGraphicsItem *other, Qt::ItemSelectionMode mode) const
{
    Q_UNUSED(mode)
    return other->type() == Bubu::Type && qFuzzyCompare(other->y(), y()) && qAbs(other->x() - x()) < 15;
}

void Shot::advance(int phase)
{
    if (!phase)
        return;
    update();
    QList<QGraphicsItem *> items = collidingItems();
    if (!items.isEmpty())
    {
        Bubu *bubu = qgraphicsitem_cast<Bubu *>(items[rand() % items.size()]);
        bubu->hp -= atk;
        delete this;
        return;
    }
    setX(x() + speed);
    if (x() > 1069)
        delete this;
}
